import { createSlice } from "@reduxjs/toolkit";
import dayjs from "dayjs";

// 创建切片
const CitySlice = createSlice({
    name: "CitySlice", //用户标记slice的名词
    // 定义初始状态
    initialState: {
        start: "北京",
        end: "广州",
        date: dayjs().format("YYYY-MM-DD"),
    },
    // 定义reducer
    reducers: {
        //什么是reducer? 定义一个方法，用来修改redux状态
        //修改城市的方法
        editChangeCity(state, { payload }) {
            state[payload.type] = payload.city;
        },
        //交换城市
        exChange(state) {
            const temp = state.start;
            state.start = state.end;
            state.end = temp;
        },
        //修改日期
        editDate(state, { payload }) {
            state.date = payload.date;
        },
    },
});

// 导出action
export const { editChangeCity, exChange, editDate } = CitySlice.actions;

// 导出reducer
export default CitySlice.reducer;
